import { Injectable } from '@nestjs/common'
import { PrismaService } from 'src/prisma/prisma.service'
import { CreateTowerDto } from './dto/create-tower.dto'
import { UpdateTowerDto } from './dto/update-tower.dto'

@Injectable()
export class TowerService {
  constructor(private prisma: PrismaService) {}

  create(createTowerDto: CreateTowerDto) {
    return this.prisma.tower.create({ data: createTowerDto })
  }

  /**
   * 获取所有的tower数据
   * @param args 查询条件
   * @returns
   */
  async findAll(args: Record<string, any>) {
    const { sectionId, search, searchStyle } = args

    return await this.prisma.tower.findMany({
      where: {
        sectionId: +sectionId,
        tName: { contains: search },
        tStyle: searchStyle || {},
      },
    })
  }

  findOne(id: number) {
    return this.prisma.tower.findFirst({ where: { id } })
  }

  /**
   * 更新的数据
   * @param id 更新的id
   * @param updateTowerDto 更新的参数
   * @returns 更新的数据
   */
  async update(id: number, updateTowerDto: UpdateTowerDto) {
    return await this.prisma.tower.update({ where: { id }, data: updateTowerDto })
  }

  remove(id: number) {
    return `This action removes a #${id} tower`
  }

  /**
   * 删除sectionId的所有数据
   * @param id 区段编号 sectionId
   * @returns 删除的sectionId的所有数据
   */
  async removeAll(id: number) {
    return await this.prisma.tower.deleteMany({ where: { sectionId: id } })
  }
}
